package com.lagou.web;

import com.lagou.domain.User;
import com.lagou.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("addStudent.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String gender = req.getParameter("gender");
        Date birthday = java.sql.Date.valueOf(req.getParameter("birthday"));

        User user = new User(username, password, gender, birthday);
        UserService us = new UserService();
        try {
            us.add(user);
            resp.sendRedirect("addSuccess.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
